package cn.dwj.hadoop.mr.flowsum;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * Created by Administrator on 2017/12/27 0027.
 */
public class FlowSunReduce extends Reducer<Text, FlowBean, Text, FlowBean>{

    //框架每传递一组数据，调用一次reduce方法
    //reduce中的业务逻辑就是遍历values，然后进行累加和再输出
    @Override
    protected void reduce(Text key, Iterable<FlowBean> values, Context context) throws IOException, InterruptedException {

        long upFlowCount = 0;
        long dFlowCount = 0;
        for(FlowBean bean : values){
            upFlowCount = upFlowCount + bean.getUpFlow();
            dFlowCount = dFlowCount + bean.getdFlow();
        }

        context.write(key, new FlowBean(key.toString(), upFlowCount, dFlowCount));
    }
}
